<template>
	<div>
		<navigation-bar>
			<div class="searchContent">
				<el-input
					v-model="drugId"
					placeholder="请输入药品id"
					size="mini"
					style="width: 150px; margin-right: 10px"
				></el-input>
				<custom-button
					type="search"
					style="margin-right: 10px"
					@searchClick="searchClick"
				></custom-button>
				<custom-button
					type="reset"
					style="margin-right: 10px"
					@resetClick="resetClick"
				></custom-button>
				<custom-button
					type="add"
					title="预生成准备记录"
					style="margin-right: 10px"
					@addClick="addClick"
				></custom-button>
				<custom-button type="print" @printClick="printClick"></custom-button>
			</div>
		</navigation-bar>
		<table-index
			:dataSource="dataSource"
			:tableColumns="tableColumns"
			:btnList="btnList"
			:pageSize="pageSize"
			:pageCount="pageCount"
			:pageNo="pageNo"
			btnListWidth="60px"
			@pageChange="pageChange"
			@editRow="editRow"
			@viewRow="viewRow"
			@deleteRow="deleteRow"
		></table-index>

		<div v-if="dialogShow">
			<custom-dialog
				:title="dialogTitle"
				size="large"
				@dialogClose="dialogClose"
			>
				<form-index
					:formConfig="formConfig"
					@submit="submit"
					:type="type"
					ref="form"
				>
					<div style="height: 300px">
						<navigation-bar customTitle="药品信息(请与给药(标签)信息保持一致)">
						</navigation-bar>
						<table-no-height
							:dataSource="contentData"
							:tableColumns="contentTableColumns"
						></table-no-height>
					</div>
				</form-index>
			</custom-dialog>
		</div>
	</div>
</template>

<script>
	import { preparationForAdministrationForm } from "@/assets/form/managementCenter/subjectManagement/testPeriodManagement/preparationForAdministrationForm"
	import customDialog from "@/components/customComponents/customDialog"
	import customButton from "@/components/customComponents/customForm/customButton"
	import formIndex from "@/components/customComponents/customForm/formIndex"
	import tableIndex from "@/components/customComponents/customeTable/tableIndex"
	import tableNoHeight from "@/components/customComponents/customeTable/tableNoHeight"
	import navigationBar from "@/components/customComponents/managementCenter/navigationBar"
	import { AjaxCommon, printAddress } from "@/libs/apiCommon"
	import { PopupCommon } from "@/libs/popupCommon"
	import { SessionCommon } from "@/libs/sessionCommon"
	export default {
		components: {
			navigationBar,
			customButton,
			tableIndex,
			customDialog,
			formIndex,
			tableNoHeight,
		},
		data() {
			return {
				drugId: "",
				// table 相关参数  begin
				dataSource: [],
				tableColumns: [
					{
						prop: "periodNo",
						label: "周期号",
						width: 80,
					},
					{
						prop: "drugSeq",
						label: "给药顺序号",
						width: 100,
					},
					{
						prop: "examineeName",
						label: "受试者姓名",
					},
					{
						prop: "randomNo",
						label: "受试者随机号",
						width: 120,
					},
					{
						prop: "drugLabel",
						label: "给药标签",
					},
					{
						prop: "labelName",
						label: "标签名称",
						width: 120,
					},
					{
						prop: "drugLabelInfo",
						label: "给药标签信息",
					},
					{
						prop: "drinkWater",
						label: "饮水量(ml)",
						width: 120,
					},
				],
				btnList: ["view"],
				pageSize: 20,
				pageCount: 100,
				pageNo: 1,
				// table 相关参数  end

				// 弹窗是否显示
				dialogShow: false,
				// 弹窗标题 (新增修改不同)
				dialogTitle: "",

				// form的配置信息
				formConfig: preparationForAdministrationForm,
				type: "add",
				contentData: [],
				contentTableColumns: [
					{
						prop: "inquiryItem",
						label: "药品名称",
					},
					{
						prop: "inquiryItemExplain",
						label: "药品规格",
					},
					{
						prop: "inquiryItemResult",
						label: "配药剂量",
					},
					{
						prop: "inquiryItemRemarks",
						label: "单位",
					},
					{
						prop: "inquiryItemRemarks",
						label: "原药品编码",
						width: 120,
					},
					{
						prop: "inquiryItemRemarks",
						label: "批号",
					},
					{
						prop: "inquiryItemRemarks",
						label: "有效期至",
					},
					{
						prop: "inquiryItemRemarks",
						label: "准备时间",
					},
					{
						prop: "inquiryItemRemarks",
						label: "给药来源描述",
						width: 140,
					},
					{
						prop: "inquiryItemRemarks",
						label: "配伍药品信息",
						width: 140,
					},
				],
			}
		},

		methods: {
			printClick() {
				let urlAdd = `${printAddress}/ccdc/tmrreport/pdf/show?1=1&_u=file:geiyaobiaoqianV1.tmrreport.xml&_i=1&_r=1
`
				let projectTestperiod =
					window.sessionStorage.getItem("projectTestperiod")
				if (projectTestperiod) {
					projectTestperiod = JSON.parse(projectTestperiod)
					urlAdd += `&pmId=${projectTestperiod.projectId}&expId=${
						projectTestperiod.testId
					}&periodNo=${
						projectTestperiod.periodId ? projectTestperiod.periodId : 1
					}`
				}

				console.log(urlAdd)
				window.open(urlAdd)
			},
			submit(d) {
				let self = this
				console.log(d)
				if (d.id) {
					AjaxCommon("XXXX/edit", "post", d)
						.then((res) => {
							if (res.data.code == 200) {
								PopupCommon.notify("success", "操作成功")
								self.initPage()
								self.dialogClose()
							} else {
								PopupCommon.notify("error", res.data.message)
							}
						})
						.catch((err) => {
							PopupCommon.notify("error", err.message)
						})
				} else {
					delete d.id
					AjaxCommon("XXXX/add", "post", d)
						.then((res) => {
							if (res.data.code == 200) {
								PopupCommon.notify("success", "操作成功")
								self.initPage()
								self.dialogClose()
							} else {
								PopupCommon.notify("error", res.data.message)
							}
						})
						.catch((err) => {
							PopupCommon.notify("error", err.message)
						})
				}
			},
			dialogClose() {
				this.dialogShow = false
			},
			searchClick() {
				this.pageNo = 1
				this.initPage()
			},
			resetClick() {
				this.drugId = ""
			},
			addClick() {
				let self = this
				PopupCommon.confirm("是否确定按试验随机表生成所有给药标签？")
					.then(() => {
						AjaxCommon(
							"/ccdc/pm/em/ing/emIngPredrug/preGenerate",
							"post",
							{}
						).then((res) => {
							console.log(res, "res...............")
							if (res.data.code == 200) {
								PopupCommon.notify("success", "生成成功")
								self.initPage()
								self.dialogClose()
							} else {
								PopupCommon.notify("error", res.data.message)
							}
						})
					})
					.catch(() => {
						console.log("c")
					})
			},
			// 分页点击事件
			pageChange(p) {
				this.pageNo = p
				this.initPage()
			},
			editRow(d) {
				this.type = "edit"
				let formConfigItem = this.formConfig
				formConfigItem.map((t) => {
					Object.keys(t).map((res) => {
						if (res == "fieldName") {
							t.filedValue = d.data[t[res]]
						}
					})
				})
				this.formConfig = formConfigItem
				this.dialogShow = true
				this.dialogTitle =
					"编辑" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
			},
			viewRow(d) {
				this.type = "look"
				let formConfigItem = this.formConfig
				formConfigItem.map((t) => {
					Object.keys(t).map((res) => {
						if (res == "fieldName") {
							t.filedValue = d.data[t[res]]
						}
					})
				})
				this.formConfig = formConfigItem
				this.dialogShow = true
				this.dialogTitle =
					"查看" + JSON.parse(SessionCommon.getCurrentMenuInfo()).title
			},
			deleteRow(d) {
				const id = d.data.id
				let self = this
				AjaxCommon("XXXX/delete?id=" + id, "delete", {
					optReason: d.optReason,
				}).then((res) => {
					console.log(res, "res...............")
					if (res.data.code == 200) {
						PopupCommon.notify("success", "删除成功")
						self.initPage()
						self.dialogClose()
					} else {
						PopupCommon.notify("error", res.data.message)
					}
				})
			},
			// 初始化事件
			initPage() {
				let self = this
				AjaxCommon("/ccdc/pm/em/ing/emIngPredrug/listByPage", "post", {
					drugId: this.drugId,
					pageNo: this.pageNo,
					pageSize: this.pageSize,
				}).then((res) => {
					if (res.data.code == 200) {
						self.dataSource = res.data.result.records
						self.pageCount = res.data.result.total
					} else {
						PopupCommon.notify("error", res.data.message)
					}
				})
			},
		},
		mounted() {
			this.initPage()
		},
	}
</script>

<style></style>
